/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.goods.dao.ZtColorDao;
import com.jeesite.modules.goods.dao.ZtSpecificationsDao;
import com.jeesite.modules.goods.entity.ZtColor;
import com.jeesite.modules.goods.entity.ZtSpecifications;
import com.jeesite.modules.test.entity.TestDataChild;

/**
 * 测试数据Service
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Service
@Transactional(readOnly = true)
public class ZtColorService extends CrudService<ZtColorDao, ZtColor> {
	
	@Autowired
	private ZtSpecificationsDao ztSpecificationsDao;
	
	
	
	@Override
	public ZtColor get(ZtColor ztColor) {
		ZtColor entity = super.get(ztColor);
		if (entity != null){
			ZtSpecifications ztSpecifications = new ZtSpecifications();
			ztSpecifications.setStatus(TestDataChild.STATUS_NORMAL);
			ztSpecifications.setZtColor(ztColor);
			entity.setZtSpecificationsList(ztSpecificationsDao.findList(ztSpecifications));
		}
		return entity;
	}
	public ZtColor getByGoods(ZtColor ztColor) {
		
		return dao.getByGoods(ztColor);
	}
	
	public ZtColor getBySku(ZtColor ztColor) {
		
		return dao.getBySku(ztColor);
	}
	
	public String getZtSpecifications(ZtSpecifications ztSpecifications) {

		ztSpecifications=ztSpecificationsDao.getByEntity(ztSpecifications);
		String ti=ztSpecifications.getName();
		
		return ti;
	}
	
	public List<ZtColor> findMh(ZtColor ztColor){
		return dao.findMh(ztColor);
	}
	
	public int findNumCount(ZtColor ztColor){
		return dao.findNumCount(ztColor);
	}

	/**
	 * 查询分页数据
	 * 
	 * @param page
	 *            分页对象
	 * @param testData
	 * @return
	 */
	@Override
	public Page<ZtColor> findPage(Page<ZtColor> page, ZtColor ztColor) {
		return super.findPage(page, ztColor);
	}
	
	@Transactional(readOnly = false)
	public void saveByOrder(ZtColor ztColor) {
		super.save(ztColor);
	
	}
	/**
	 * 保存数据（插入或更新）
	 * 
	 * @param testData
	 */
	@Override
	@Transactional(readOnly = false)
	public void save(ZtColor ztColor) {
		
		// 保存上传图片
		Integer numCount=ztColor.getNumCount();
		
		if(numCount == null || numCount<0){
			numCount=0;
		}
		
		ztColor.setNumCount(numCount);
		
		super.save(ztColor);
		FileUploadUtils.saveFileUpload(ztColor.getId(), "user_upload2");
		int count=0;
		// 保存 TestData子表
				for (ZtSpecifications ztSpecifications : ztColor.getZtSpecificationsList()){
					if (!"1".equals(ztSpecifications.getStatus())){
						ztSpecifications.setZtColor(ztColor);
						if (ztSpecifications.getIsNewRecord()){//新增
							ztSpecifications.preInsert();
							
							if(StringUtils.isBlank(ztSpecifications.getSku())){
								ztSpecifications.setSku(ztColor.getSku());
							}
							if(StringUtils.isBlank(ztSpecifications.getPname())){
								ztSpecifications.setPname(ztColor.getPname());
							}
							
							ztSpecificationsDao.insert(ztSpecifications);
							count+=ztSpecifications.getStock();
						}else{//编辑
							ztSpecifications.preUpdate();
							if(StringUtils.isBlank(ztSpecifications.getSku())){
								ztSpecifications.setSku(ztColor.getSku());
							}
							if(StringUtils.isBlank(ztSpecifications.getPname())){
								ztSpecifications.setPname(ztColor.getPname());
							}
							ztSpecificationsDao.update(ztSpecifications);
							count+=ztSpecifications.getStock();
						}
					}else{//删除
						ztSpecificationsDao.delete(ztSpecifications);
						count-=ztSpecifications.getStock();
					}
				}
		if(count>0){
			ztColor.setNumCount(count);
			super.save(ztColor);
		}
	}
	@Transactional(readOnly = false)
	public void updateZtGoodsid(ZtColor ztColor) {
		dao.updateZtGoodsid(ztColor);
		
	}
	
	public  List<ZtSpecifications> getZtSpecifications(ZtColor ztColor) {
		
		ZtSpecifications ztSpecifications=new ZtSpecifications();
		ztSpecifications.setZtColor(ztColor);
		return ztSpecificationsDao.findList(ztSpecifications);
		
	}
	
	

	/**
	 * 更新状态
	 * 
	 * @param testData
	 */
	@Override
	@Transactional(readOnly = false)
	public void updateStatus(ZtColor ztColor) {
		super.updateStatus(ztColor);
	}


	/**
	 * 删除数据
	 * 
	 * @param testData
	 */
	@Override
	@Transactional(readOnly = false)
	public void delete(ZtColor ztColor) {
		super.delete(ztColor);
	}

}